##########################################
###Autocratic Elections
###Stabilizing Tool or Force for Change
###Replication of core tables
#########################################
rm(list = ls())
library(foreign);library(pspline);library(survival);library(MCMCpack)
library(ZeligGAM);library(R2jags)
library(mgcv);library(glmnet)
library(bootstrap);library(plyr);library(stargazer)

data <- read.dta("ReplicationData.dta")
dataallelections <- read.dta("ReplicationDataAllElections.dta")


##Table 1 in paper


logit.model.3 <- glm(gwf_fail ~ proxelection1 + proxelection8 + regiondem + loggdp + gdp_grow + 
                     cow_milsize + resdep2 +  
                       duration + duration2 + duration3 ,
                   data=data,family=binomial(link = "logit"))
summary(logit.model.3)


logit.model.4 <- glm(gwf_fail ~ election + election5year + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + 
                       duration + duration2 + duration3,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.4)


logit.model.5 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + 
                       duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                     data=data,family=binomial(link = "logit"))
summary(logit.model.5)

logit.model.6 <- glm(gwf_fail ~ election + election5year + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +  
                       duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                     data=data,family=binomial(link = "logit"))
summary(logit.model.6)


logit.model.7 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +   
                       duration + duration2 + duration3 + as.factor(region) + as.factor(decade) + sip2,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.7)

logit.model.8 <- glm(gwf_fail ~ election + election5year + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + 
                       duration + duration2 + duration3 + as.factor(region) + as.factor(decade) + sip2,
                     data=data,family=binomial(link = "logit"))
summary(logit.model.8)


logit.model.9 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 +   
                       duration + duration2 + duration3 + as.factor(region) + as.factor(decade) + sip2,
                     data=dataallelections,family=binomial(link = "logit"))
summary(logit.model.9)

logit.model.10 <- glm(gwf_fail ~ election + election5year + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + 
                       duration + duration2 + duration3 + as.factor(region) + as.factor(decade) + sip2,
                     data=dataallelections,family=binomial(link = "logit"))
summary(logit.model.10)

stargazer(logit.model.3,logit.model.4,logit.model.5,logit.model.6,logit.model.7,
          logit.model.8,logit.model.9,logit.model.10,
          out="../Results/LogitResultsv2.tex",dep.var.caption="",dep.var.labels="Regime failure",
          no.space=TRUE,align=TRUE, float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8", 
                               "Election" , "Election 5 year", 
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence",
                               "Duration","Duration$^2$","Duration$^3$",
                               "Region 2","Region 3","Region 4","Region 5","Region 6","Region 7","Region 8",
                               "Decade 2","Decade 3","Decade 4","Decade 5","Decade 6",
                               "SIP 2"),
      #    report="vc*t",
          column.labels=c("1","2","3","4","5","6","All","All"),
          notes="Logit regressions with Geddes-Wright-Frantz (GWF; 2014) regime failure as dependent variable.")


##End table 1 in paper

##Table 2 (robustness test table) in paper

logit.model.4.1 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + gwf_personal + gwf_military + gwf_monarch +
                         duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                     data=data,family=binomial(link = "logit"))
summary(logit.model.4.1)

logit.model.4.2 <- glm(gwf_fail ~ proxelection1 + regiondem + loggdp + gdp_grow + 
                         cow_milsize + resdep2  + electioncount +
                         duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                       data=data,family=binomial(link = "logit"))
summary(logit.model.4.2)

logit.model.4.3 <- glm(gwf_fail ~ proxelection1 + regiondem + loggdp + gdp_grow + 
                         cow_milsize + resdep2  + lntimesincefirstelection +
                         duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                       data=data,family=binomial(link = "logit"), subset=data$timesincefirstelection>=7)
summary(logit.model.4.3)


logit.model.4.4 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                        cow_milsize + resdep2 + 
                         duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                       data=data,family=binomial(link = "logit"), subset=data$multipartyelection==1)
summary(logit.model.4.4)

logit.model.4.5 <- glm(gwf_fail ~ proxelection1 + proxelection8  + regiondem + loggdp + gdp_grow + 
                       cow_milsize + resdep2 + 
                         duration + duration2 + duration3 + as.factor(region) + as.factor(decade),
                     data=data,family=binomial(link = "logit"), subset=data$gwf_democratization==0)
summary(logit.model.4.5)


stargazer(logit.model.4.1,logit.model.4.2,logit.model.4.3,logit.model.4.4,logit.model.4.5,
          out="../Results/LogitResultsTable2.tex",dep.var.caption="",dep.var.labels="Regime failure",
          no.space=TRUE,align=TRUE, float=FALSE,font.size="tiny",
          covariate.labels = c("Proximity to election / 1 ", "Proximity to election / 8",  
                               "Region Polity","ln(GDP per capita)", 
                               "GDP Growth", "Military size","Resource dependence",
                               "Personalist", "Military", "Monarchy",
                               "sum(election)","Time since first",
                               "Duration","Duration$^2$","Duration$^3$"),
          notes="Logit regressions with Geddes-Wright-Frantz (GWF; 2014) regime failure as dependent variable.",
          column.labels=c("1","2","3","Multiparty only","No Democ") )



##End table 2 in paper




